package wanbang.dao.Imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import wanbang.dao.UserDao;
import wanbang.entities.User;
@Repository
public class UserDaoImp implements UserDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public User selectByUserNameAndPassword(String userName, String passWord) {
		String sql = "select user_id,user_name,user_blance from user where user_account = ? and user_password = ?";
		RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
		 User user = jdbcTemplate.queryForObject(sql, rowMapper, userName,passWord);
		return user;
	}

	@Override
	public void selectUserBlanceByPrice(Integer userId, Double price) {
		// TODO Auto-generated method stub
		String sql ="update user set user_blance = user_blance - ? where user_id = ?";
		this.jdbcTemplate.update(sql, price,userId);
	}

	@Override
	public Double selectUserBlanceByUserId(Integer userId) {
		// TODO Auto-generated method stub
		String sql = "select user_blance from user where user_id = ? ";
		return this.jdbcTemplate.queryForObject(sql, Double.class, userId);
	}

	@Override
	public boolean insertIntoUserBalance(Double money, Integer userId) {
		// TODO Auto-generated method stub
		boolean flag = false;
		String sql = "update user set user_blance = user_blance + ? where user_id = ? ";
		int count = this.jdbcTemplate.update(sql, money,userId);
		if(count > 0) {
			flag = true;
		}
		return flag;
	}




	



}
